const BinaryTree = require("../utils/binaryTree")
let arr = [3, 5, 1, 6, 2, 0, 8, null, null, 7, 4];
let rootObj = new BinaryTree(arr);

const p = rootObj.findNode(6);
const q = rootObj.findNode(8);
var lowestCommonAncestor = function (root, p, q) {
    if (!root || root === p || root === q) {
        return root
    }
    let left = lowestCommonAncestor(root.left, p, q)
    let right = lowestCommonAncestor(root.right, p, q)
    if (left && right) {
        return root
    }
    return left ? left : right
};

const result = lowestCommonAncestor(rootObj.root, p, q);
console.log("🚀 ~ result:", result)
